﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class RepaymentPlanBiz
    {
        ///<summary>
        /// 实现数据的四舍五入法
        ///</summary>
        ///<param name="v">要进行处理的数据</param>
        ///<param name="x">保留的小数位数</param>
        ///<returns>四舍五入后的结果</returns>
        public static decimal Round(decimal v, int x)
        {
            bool isNegative = false;
            //如果是负数
            if (v < 0)
            {
                isNegative = true;
                v = -v;
            }

            int IValue = 1;
            for (int i = 1; i <= x; i++)
            {
                IValue = IValue * 10;
            }
            decimal Int = Math.Floor(v * IValue + 0.5m);
            v = Int / IValue;

            if (isNegative)
            {
                v = -v;
            }

            return v;
        }

        /// <summary>
        /// 构建还款计划
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public static List<RepaymentPlan> BuildRepaymentPlan(int projectID)
        {
            try
            {
                List<RepaymentPlan> repaymentPlans = new List<RepaymentPlan>();
                List<RepaymentPlanItem> repaymentPlanItems = null;

                DateTime startDate = DateTime.Now;

                var project = new Project().Find(projectID.ToString());
                var policyList = new List<Policy>();
                using (Context ctx = new Context())
                {
                    policyList = (from c in ctx.Policy
                                  join d in ctx.ProjectPolicy
                                  on c.ID equals d.PolicyId
                                  where d.ProjectId == projectID
                                  select new { c })
                                .ToList()
                                .Select(p => new Policy().Set(p.c)).ToList();

                }
                if (project == null || policyList == null || policyList.Count == 0)
                {
                    return null;
                }
                else
                {
                    int i = 1;
                    var commonPolicy = policyList.Where(c => !c.IsAttach).FirstOrDefault();
                    decimal lastRate = project.OriginalRate;//借款利率
                    foreach (var item in policyList)
                    {
                        if (item.IsAttach && item.SettlementType == SettlementType.Proportion)
                        {
                            lastRate = item.Rate;
                        }
                        var repaymentPlan = new RepaymentPlan();
                        repaymentPlan.Policy = item;
                        int cycleAmount = item.CycleAmount;
                        decimal repaymentInterest = 0m;
                        decimal repaymentPrincipal = 0m;
                        if (item.CycleAmount == 0)
                        {
                            cycleAmount = project.CycleAmount;
                        }
                         
                        decimal duration = 0;
                        DurationType durationType = DurationType.ByMonth;
                        if (item.Duration == 0)
                        {
                            duration = commonPolicy.Duration;
                            durationType = commonPolicy.DurationType;
                        }
                        else
                        {
                            duration = item.Duration;
                            durationType = item.DurationType;
                        }

                        decimal totalDay = 0;
                        if (durationType == DurationType.ByDay)
                        {
                            totalDay = duration * cycleAmount;
                        }
                        else
                        {
                            totalDay = startDate.AddMonths((int)duration * cycleAmount).Subtract(startDate).Days;
                        }

                        repaymentPlan.UserName = "";
                        repaymentPlan.ProjectId = projectID;
                        repaymentPlan.PolicyId = item.ID;
                        repaymentPlan.CycleAmount = cycleAmount;
                        repaymentPlan.Rate = item.Rate;
                        if (item.IsAttach)//特殊策略
                        {
                            repaymentPlan.UserName = "system";
                            repaymentPlan.RepaymentPrincipal = 0;
                            repaymentPrincipal = 0;
                        }
                        else
                        {
                            repaymentPrincipal = project.Financing;
                            repaymentPlan.RepaymentPrincipal = project.Financing;
                        }

                        if (item.SettlementType == SettlementType.Proportion)//按比例
                        {
                            repaymentInterest = Round((project.Financing * lastRate / 100 / 365) * totalDay, 2);
                            repaymentPlan.RepaymentInterest = repaymentInterest;
                        }
                        else if (item.SettlementType == SettlementType.Fixed)//固定值
                        {
                            repaymentInterest = item.Rate;
                            repaymentPlan.RepaymentInterest = item.Rate;
                        }
                        repaymentPlan.ID = i;
                        //处理明细
                        decimal spanDay = 0;

                        int j = 0;
                        repaymentPlanItems = new List<RepaymentPlanItem>();
                        RepaymentPlanItem repaymentPlanItem = null;
                        decimal repaymentInterestTemp = repaymentInterest;
                        decimal repaymentPrincipalTemp = repaymentPrincipal;
                        switch (item.BalanceType)
                        {
                            #region 等额本金算法
                            //case BalanceType.AverCapitalInterest://等额本金算法

                            //    repaymentPrincipalTemp = repaymentPrincipal;
                            //    decimal monthRate = lastRate / 12;
                            //    repaymentInterest = 0;
                            //    for (int k = 0; k < cycleAmount; k++)
                            //    {
                            //        repaymentInterest = repaymentInterest + repaymentPrincipalTemp * (monthRate / 100) * duration;
                            //        repaymentPrincipalTemp = repaymentPrincipalTemp - repaymentPrincipal / cycleAmount;
                            //    }
                            //    repaymentPlan.RepaymentInterest = repaymentInterest;

                            //    repaymentInterestTemp = repaymentInterest;
                            //    repaymentPrincipalTemp = repaymentPrincipal;
                            //    for (j = 0; j < cycleAmount; j++)
                            //    {
                            //        repaymentPlanItem = new RepaymentPlanItem();
                            //        if (durationType == DurationType.ByDay)
                            //        {
                            //            repaymentPlanItem.RepaymentDate = startDate.AddDays((int)duration * (j + 1));
                            //            spanDay = (int)duration;
                            //        }
                            //        else if (durationType == DurationType.ByMonth)
                            //        {
                            //            repaymentPlanItem.RepaymentDate = startDate.AddMonths((int)duration * (j + 1));
                            //            spanDay = startDate.AddMonths((int)duration * (j + 1)).Subtract(
                            //                startDate.AddMonths((int)duration * j)).Days;
                            //        }

                            //        repaymentPlanItem.SpanDay = (int)spanDay;
                            //        repaymentPlanItem.PlanID = i;
                            //        repaymentPlanItem.CycleNumber = j + 1;
                            //        if (j == cycleAmount - 1)
                            //        {
                            //            repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                            //            repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                            //            repaymentPlanItem.ReMainPrincipal = 0;
                            //            repaymentPlanItem.ReMainInterest = 0;
                            //        }
                            //        else
                            //        {
                            //            decimal currentCycleInterest = Round(repaymentInterest / cycleAmount, 2);
                            //            //if (item.SettlementType == SettlementType.Proportion)
                            //            //{
                            //            //    currentCycleInterest = Round((repaymentPrincipalTemp * (lastRate / 100 / 365)) * spanDay, 2);
                            //            //}
                            //            decimal currentCyclePrincipal = Round(repaymentPrincipal / cycleAmount, 2);
                            //            repaymentPlanItem.RepaymentInterest = currentCycleInterest;
                            //            repaymentPlanItem.RepaymentPrincipal = currentCyclePrincipal;
                            //            repaymentPrincipalTemp = repaymentPrincipalTemp - currentCyclePrincipal;
                            //            repaymentInterestTemp = repaymentInterestTemp - currentCycleInterest;
                            //            repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                            //            repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                            //        }
                            //        repaymentPlanItems.Add(repaymentPlanItem);
                            //    }
                            //    break;
                            #endregion
                            case BalanceType.AverCapitalInterest://等额本息算法
                                 
                                decimal monthRate = 0;
                                decimal monthtotal = 0;
                                if (item.SettlementType == SettlementType.Proportion)
                                {
                                    monthRate = lastRate / 12 / 100;
                                    repaymentInterest = 0;
                                    //每月月供额=〔贷款本金×月利率×(1＋月利率)＾还款月数〕÷〔(1＋月利率)＾还款月数-1〕
                                    monthtotal = (repaymentPrincipal * monthRate * (decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount)) / ((decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount) - 1);
                                    //总利息=还款月数×每月月供额-贷款本金
                                    repaymentInterest = cycleAmount * monthtotal - repaymentPrincipal;

                                    repaymentPlan.RepaymentInterest = repaymentInterest;
                                }

                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    if (durationType == DurationType.ByDay)
                                    {
                                        repaymentPlanItem.RepaymentDate = startDate.AddDays((int)duration * (j + 1));
                                        spanDay = (int)duration;
                                    }
                                    else if (durationType == DurationType.ByMonth)
                                    {
                                        repaymentPlanItem.RepaymentDate = startDate.AddMonths((int)duration * (j + 1));
                                        spanDay = startDate.AddMonths((int)duration * (j + 1)).Subtract(
                                            startDate.AddMonths((int)duration * j)).Days;
                                    } 

                                    repaymentPlanItem.SpanDay = (int)spanDay;
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    //每月月供额=〔贷款本金×月利率×(1＋月利率)＾还款月数〕÷〔(1＋月利率)＾还款月数-1〕
                                    //每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
                                    //每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
                                    //总利息=还款月数×每月月供额-贷款本金

                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        if (item.SettlementType == SettlementType.Proportion)
                                        {
                                            //每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
                                            decimal monthInterest = repaymentPrincipal * monthRate * ((decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount) - (decimal)Math.Pow((double)(1 + monthRate), (double)j)) / ((decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount) - 1);

                                            //每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
                                            decimal monthPrincipal = repaymentPrincipal * monthRate * (decimal)Math.Pow((double)(1 + monthRate), (double)j) / ((decimal)Math.Pow((double)(1 + monthRate), (double)cycleAmount) - 1);

                                            repaymentPlanItem.RepaymentInterest = Round(monthInterest, 2);
                                            repaymentPlanItem.RepaymentPrincipal = monthPrincipal;
                                            repaymentPrincipalTemp = repaymentPrincipalTemp - Round(monthPrincipal, 2);
                                            repaymentInterestTemp = repaymentInterestTemp - Round(monthInterest, 2);
                                            repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                            repaymentPlanItem.ReMainInterest = repaymentInterestTemp;
                                        }
                                        else
                                        {
                                            decimal currentCycleInterest = Round(repaymentInterest / cycleAmount, 2);
                                             
                                            decimal currentCyclePrincipal = Round(repaymentPrincipal / cycleAmount, 2);
                                            repaymentPlanItem.RepaymentInterest = currentCycleInterest;
                                            repaymentPlanItem.RepaymentPrincipal = 0;
                                            repaymentInterestTemp = repaymentInterestTemp - currentCycleInterest;
                                            repaymentPrincipalTemp = repaymentPrincipalTemp - currentCyclePrincipal;
                                            repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                            repaymentPlanItem.ReMainInterest = repaymentInterestTemp;
                                        }

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                            case BalanceType.EachInterestTerminalCapital://每期结息，到期还本
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    if (durationType == DurationType.ByDay)
                                    {
                                        repaymentPlanItem.RepaymentDate = startDate.AddDays((int)duration * (j + 1));
                                        spanDay = (int)duration;
                                    }
                                    else if (durationType == DurationType.ByMonth)
                                    {

                                        repaymentPlanItem.RepaymentDate = startDate.AddMonths((int)duration * (j + 1));
                                        spanDay = startDate.AddMonths((int)duration * (j + 1)).Subtract(
                                            startDate.AddMonths((int)duration * j)).Days;
                                    }
                                    repaymentPlanItem.SpanDay = (int)spanDay;
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    { 
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        decimal currentCycleInterest = Round(repaymentInterest / cycleAmount, 2);
                                        if (item.SettlementType == SettlementType.Proportion)
                                        {
                                            currentCycleInterest = Round((repaymentPrincipal * (lastRate / 100 / 365)) * spanDay, 2);
                                        }
                                        decimal currentCyclePrincipal = Round(repaymentPrincipal / cycleAmount, 2);
                                        repaymentPlanItem.RepaymentInterest = currentCycleInterest;
                                        repaymentPlanItem.RepaymentPrincipal = 0; 
                                         
                                        repaymentInterestTemp = repaymentInterestTemp - currentCycleInterest;

                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                            case BalanceType.TerminalCapitalInterest://到期结本息
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    if (durationType == DurationType.ByDay)
                                    {
                                        repaymentPlanItem.RepaymentDate = startDate.AddDays((int)duration * (j + 1));
                                        spanDay = (int)duration;
                                    }
                                    else if (durationType == DurationType.ByMonth)
                                    {

                                        repaymentPlanItem.RepaymentDate = startDate.AddMonths((int)duration * (j + 1));
                                        spanDay = startDate.AddMonths((int)duration * (j + 1)).Subtract(
                                            startDate.AddMonths((int)duration * j)).Days;
                                    }
                                    repaymentPlanItem.SpanDay = (int)spanDay;
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = 0;
                                        repaymentPlanItem.RepaymentPrincipal = 0;
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }

                                break;
                            case BalanceType.EverageFixed://针对固定，每期平均
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    if (durationType == DurationType.ByDay)
                                    {
                                        repaymentPlanItem.RepaymentDate = startDate.AddDays((int)duration * (j + 1));
                                        spanDay = (int)duration;
                                    }
                                    else if (durationType == DurationType.ByMonth)
                                    {

                                        repaymentPlanItem.RepaymentDate = startDate.AddMonths((int)duration * (j + 1));
                                        spanDay = startDate.AddMonths((int)duration * (j + 1)).Subtract(
                                            startDate.AddMonths((int)duration * j)).Days;
                                    }
                                    repaymentPlanItem.SpanDay = (int)spanDay;
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        decimal currentCycleInterest = Round(repaymentPrincipal / cycleAmount, 2);
                                        //if (item.SettlementType == SettlementType.Proportion)
                                        //{
                                        //    currentCycleInterest = Round((repaymentPrincipal * (lastRate / 100 / 365)) * spanDay, 2);
                                        //}
                                        decimal currentCyclePrincipal = Round(repaymentPrincipal / cycleAmount, 2);
                                        repaymentPlanItem.RepaymentInterest = currentCycleInterest;
                                        repaymentPlanItem.RepaymentPrincipal = currentCyclePrincipal;
                                        repaymentPrincipalTemp = repaymentPrincipalTemp - currentCyclePrincipal;
                                        repaymentInterestTemp = repaymentInterestTemp - currentCycleInterest;
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                            case BalanceType.TerminalFixed://针对固定，到期付清
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    if (durationType == DurationType.ByDay)
                                    {
                                        repaymentPlanItem.RepaymentDate = startDate.AddDays((int)duration * (j + 1));
                                        spanDay = (int)duration;
                                    }
                                    else if (durationType == DurationType.ByMonth)
                                    {

                                        repaymentPlanItem.RepaymentDate = startDate.AddMonths((int)duration * (j + 1));
                                        spanDay = startDate.AddMonths((int)duration * (j + 1)).Subtract(
                                            startDate.AddMonths((int)duration * j)).Days;
                                    }
                                    repaymentPlanItem.SpanDay = (int)spanDay;
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = 0;
                                        repaymentPlanItem.RepaymentPrincipal = 0;
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                        }
                        repaymentPlan.RepaymentPlanItemList = repaymentPlanItems;
                        repaymentPlans.Add(repaymentPlan);
                        i++;
                    }

                }
                return repaymentPlans;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }
        /// <summary>
        /// 融资完成的时候调用
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public static List<RepaymentPlan> CreateRepaymentPlan(int projectID)
        {
            try
            {
                List<RepaymentPlan> repaymentPlans = new List<RepaymentPlan>();
                List<RepaymentPlanItem> repaymentPlanItems = null;


                var project = new Project().Find(projectID.ToString());
                var policyList = new List<Policy>();
                using (Context ctx = new Context())
                {
                    policyList = (from c in ctx.Policy
                                  join d in ctx.ProjectPolicy
                                  on c.ID equals d.PolicyId
                                  where d.ProjectId == projectID
                                  select new { c })
                                .ToList()
                                .Select(p => new Policy().Set(p.c)).ToList();

                }
                if (project == null || policyList == null || policyList.Count == 0)
                {
                    return null;
                }
                else
                {
                    int i = 1;
                    var commonPolicy = policyList.Where(c => !c.IsAttach).FirstOrDefault();
                    foreach (var item in policyList)
                    {

                        var repaymentPlan = new RepaymentPlan();
                        repaymentPlan.Policy = item;
                        int cycleAmount = item.CycleAmount;
                        decimal repaymentInterest = 0m;
                        decimal repaymentPrincipal = 0m;
                        if (item.CycleAmount == 0)
                        {
                            cycleAmount = project.CycleAmount;
                        }

                        decimal monthCount = 0;
                        decimal duration = 0;
                        DurationType durationType = DurationType.ByMonth;
                        if (item.Duration == 0)
                        {
                            duration = commonPolicy.Duration;
                            durationType = commonPolicy.DurationType;
                        }
                        else
                        {
                            duration = item.Duration;
                            durationType = item.DurationType;
                        }
                        if (durationType == DurationType.ByDay)
                        {

                            monthCount = Math.Round((((decimal)(cycleAmount * duration)) / 365) * 12, 2, MidpointRounding.AwayFromZero);
                        }
                        else
                        {

                            monthCount = cycleAmount * duration;
                        }


                        repaymentPlan.UserName = project.BorrowerName;
                        repaymentPlan.ProjectId = projectID;
                        repaymentPlan.PolicyId = item.ID;
                        repaymentPlan.CycleAmount = cycleAmount;
                        repaymentPlan.Rate = item.Rate;
                        if (item.IsAttach)//特殊策略
                        {
                            repaymentPlan.UserName = "System";
                            repaymentPlan.RepaymentPrincipal = 0;
                            repaymentPrincipal = 0;
                        }
                        else
                        {
                            repaymentPrincipal = project.Financing;
                            repaymentPlan.RepaymentPrincipal = project.Financing;
                        }

                        if (item.SettlementType == SettlementType.Proportion)//按比例
                        {
                            repaymentInterest = Math.Round((project.Financing * item.Rate / 100) * (monthCount / 12), 2, MidpointRounding.AwayFromZero);
                            repaymentPlan.RepaymentInterest = repaymentInterest;
                        }
                        else if (item.SettlementType == SettlementType.Fixed)//固定值
                        {
                            repaymentInterest = item.Rate;
                            repaymentPlan.RepaymentInterest = item.Rate;
                        }
                        repaymentPlan.ID = i;
                        //处理明细
                        int j = 0;
                        repaymentPlanItems = new List<RepaymentPlanItem>();
                        RepaymentPlanItem repaymentPlanItem = null;
                        decimal repaymentInterestTemp = repaymentInterest;
                        decimal repaymentPrincipalTemp = repaymentPrincipal;
                        switch (item.BalanceType)
                        {
                            case BalanceType.AverCapitalInterest://等额本息
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = Math.Round(repaymentInterest / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPlanItem.RepaymentPrincipal = Math.Round(repaymentPrincipal / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPrincipalTemp = repaymentPrincipalTemp - Math.Round(repaymentPrincipal / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentInterestTemp = repaymentInterestTemp - Math.Round(repaymentInterest / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                            case BalanceType.EachInterestTerminalCapital://每期结息，到期还本
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = Math.Round(repaymentInterest / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPlanItem.RepaymentPrincipal = 0;

                                        repaymentInterestTemp = repaymentInterestTemp - Math.Round(repaymentInterest / cycleAmount, 2, MidpointRounding.AwayFromZero);

                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                            case BalanceType.TerminalCapitalInterest://到期结本息
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = 0;
                                        repaymentPlanItem.RepaymentPrincipal = 0;
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }

                                break;
                            case BalanceType.EverageFixed://针对固定，每期平均
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = Math.Round(repaymentInterest / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPlanItem.RepaymentPrincipal = Math.Round(repaymentPrincipal / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPrincipalTemp = repaymentPrincipalTemp - Math.Round(repaymentPrincipal / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentInterestTemp = repaymentInterestTemp - Math.Round(repaymentInterest / cycleAmount, 2, MidpointRounding.AwayFromZero);
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                            case BalanceType.TerminalFixed://针对固定，到期付清
                                repaymentInterestTemp = repaymentInterest;
                                repaymentPrincipalTemp = repaymentPrincipal;
                                for (j = 0; j < cycleAmount; j++)
                                {
                                    repaymentPlanItem = new RepaymentPlanItem();
                                    repaymentPlanItem.PlanID = i;
                                    repaymentPlanItem.CycleNumber = j + 1;
                                    if (j == cycleAmount - 1)
                                    {
                                        repaymentPlanItem.RepaymentInterest = repaymentInterestTemp;
                                        repaymentPlanItem.RepaymentPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainPrincipal = 0;
                                        repaymentPlanItem.ReMainInterest = 0;
                                    }
                                    else
                                    {
                                        repaymentPlanItem.RepaymentInterest = 0;
                                        repaymentPlanItem.RepaymentPrincipal = 0;
                                        repaymentPlanItem.ReMainPrincipal = repaymentPrincipalTemp;
                                        repaymentPlanItem.ReMainInterest = repaymentInterestTemp;

                                    }
                                    repaymentPlanItems.Add(repaymentPlanItem);
                                }
                                break;
                        }
                        repaymentPlan.RepaymentPlanItemList = repaymentPlanItems;
                        repaymentPlans.Add(repaymentPlan);
                        i++;
                    }

                }
                return repaymentPlans;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }
    }
}
